

package com.appview;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.appdatebase.CheckUserInfo;


public class StuLogin extends JFrame implements ActionListener {
	private JPanel jPanel, jPanel1, jPanel2, jPanel3, jPanel4, jPanel5;
	private JLabel jLabel1, jLabel2, jLabel3;
	private JButton jButton1, jButton2, jButton3;
	private JTextField jField1;
	private JPasswordField jPasswordField;
	private JRadioButton rButton1, rButton2, rButton3;
	private JCheckBox jBox1, jBox2, jBox3;
	private ButtonGroup bGroup;

	public static void main(String[] args) {
		new StuLogin();
	}
	public StuLogin() {
		//设置文字
		jLabel1 = new JLabel("学号:");
		jLabel2 = new JLabel("密码:");
		jLabel3 = new JLabel("欢迎使用教学信息管理系统");

		//设置文本框
		jField1 = new JTextField(13);
		jPasswordField = new JPasswordField(13);

		//设置复选框
		jBox1 = new JCheckBox("记住密码");
		jBox2 = new JCheckBox("自动登录");

		//设置单选框
		rButton1 = new JRadioButton("管理员");
		rButton2 = new JRadioButton("教师");
		rButton3 = new JRadioButton("学生");
		rButton3.setSelected(true);

		//设置单选框只能单选
		bGroup = new ButtonGroup();
		bGroup.add(rButton1);
		bGroup.add(rButton2);
		bGroup.add(rButton3);

		//设置按钮
		jButton1 = new JButton("登陆");
		jButton2 = new JButton("退出");
		jButton3 = new JButton("重置");
		jButton1.addActionListener(this);
		jButton2.addActionListener(this);
		jButton3.addActionListener(this);

		//添加组件
		jPanel = new JPanel();
		jPanel1 = new JPanel();
		jPanel2 = new JPanel();
		jPanel4 = new JPanel();
		jPanel5 = new JPanel();
		jPanel3 = new JPanel();

		jPanel.add(jLabel3);
		jPanel1.add(jLabel1, BorderLayout.WEST);
		jPanel1.add(jField1, BorderLayout.CENTER);
		jPanel2.add(jLabel2, BorderLayout.WEST);
		jPanel2.add(jPasswordField, BorderLayout.CENTER);

		jPanel4.add(jBox1);
		jPanel4.add(jBox2);
		jPanel4.setLayout(new FlowLayout(FlowLayout.CENTER));

		jPanel5.add(rButton1);
		jPanel5.add(rButton2);
		jPanel5.add(rButton3);
		jPanel5.setLayout(new FlowLayout(FlowLayout.CENTER));

		jPanel3.add(jButton1);
		jPanel3.add(jButton2);
		jPanel3.add(jButton3);
		jPanel3.setLayout(new FlowLayout(FlowLayout.CENTER));

		//添加组件
		this.add(jPanel);
		this.add(jPanel1);
		this.add(jPanel2);
		this.add(jPanel4);
		this.add(jPanel5);
		this.add(jPanel3);
		this.setLayout(new GridLayout(6, 1));

		//设置界面
		this.setTitle("登录系统");
		this.setSize(310, 270);
		this.setLocation(450, 250);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	//按钮监听
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jButton1) {
			//连接数据库
			//创建检查账号密码对象
			CheckUserInfo checkUserInfo = new CheckUserInfo();
			//判断检查的返回值
			boolean isLogin =
					checkUserInfo.checkInfo
							(jField1.getText().toString(), jPasswordField.getText().toString());
			//返回值如果true
			if (isLogin) {
				new MainView(jField1.getText().toString());
				StuLogin.this.dispose();
			}else {  //否则
				JOptionPane.showMessageDialog
						(StuLogin.this, "账号或密码有误，请重新输入", "无法登录", JOptionPane.WARNING_MESSAGE);
			}

		}else if (e.getSource() == jButton2) {
			StuLogin.this.dispose();
		}else if (e.getSource() == jButton3) {
			jField1.setText("");
			jPasswordField.setText("");
		}
	}
}
